package com.origin.aidllibrary.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.Nullable;

import com.origin.aidllibrary.IMyAidlInterface;
import com.origin.aidllibrary.IServiceMsgCallBack;
import com.origin.utils.FileUtils;
import com.origin.utils.PathUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyService extends Service {
    private static String name;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        name = getPackageName();
        return new MyBinder();
    }


    public static String startSendFileTag = "0x123";
    public static String canSendFileTag = "0x124";
    public static String fileHeadTag = "0x125";
    public static String fileTailTag = "0x126";
    public static String sendFileFinishTag = "0x127";


    static class MyBinder extends IMyAidlInterface.Stub {


        private boolean isStartReceive;
        IServiceMsgCallBack _serviceMsgCallBack;

        @Override
        public String getName() throws RemoteException {
            return name + "  test";
        }

        @Override
        public void sendMessage(String str) throws RemoteException {
            System.out.println("sendMessage: " + str);
            if (isStartReceive) {
                if (str.equals(sendFileFinishTag)) {
                    sendFileFinish();
                    return;
                }
                receiveFile(str);
                return;
            }
            if (str.equals(startSendFileTag)) {
                isStartReceive = true;
                fileBytes.clear();
                serviceSendMessage(canSendFileTag);
            }
        }

        @Override
        public void setServiceMsgCallBack(IServiceMsgCallBack serviceMsgCallBack) throws RemoteException {
            _serviceMsgCallBack = serviceMsgCallBack;
        }

        public void serviceSendMessage(String str) throws RemoteException {
            if (_serviceMsgCallBack != null) {
                _serviceMsgCallBack.receiveMessage(str);
            }
        }

        String pattern = "(^0x125)(\\[(-?(\\w*),?)*\\])(0x126$)";


        List<List<Byte>> fileBytes = new ArrayList<>();

        public void receiveFile(String str) {
            List<Byte> bytes = new ArrayList<>();
            Pattern r = Pattern.compile(pattern);
            str = str.replace(" ", "");
            Matcher m = r.matcher(str);
            if (m.find()) {
                String value = m.group(2);
                String[] valueArray = value.replace("[", "").replace("]", "").split(",");
                for (String s : valueArray) {
                    bytes.add(((byte) Integer.parseInt(s)));
                }
            } else {
                System.out.println("NO MATCH");
            }
            fileBytes.add(bytes);
        }

        public void sendFileFinish() {
            List<Byte> bytes = new ArrayList<>();
            for (List<Byte> fileByte : fileBytes) {
                for (Byte aByte : fileByte) {
                    bytes.add(aByte);
                }
            }
            byte[] bytesArray = listTobyte(bytes);
            FileUtils.byteToFile(bytesArray, PathUtils.filesPath + "/2.png");
        }

        private byte[] listTobyte(List<Byte> list) {
            if (list == null || list.size() < 0)
                return null;
            byte[] bytes = new byte[list.size()];
            int i = 0;
            Iterator<Byte> iterator = list.iterator();
            while (iterator.hasNext()) {
                bytes[i] = iterator.next();
                i++;
            }
            return bytes;
        }
    }


}
